// pages/activesDetail/index.js

import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({
  data: {
    singleActiveDetailArr: [],
    isCollect: false,
  },

  onLoad: function (options) {
    console.log(options);
    let id = parseInt(this.options.id);
    this.getActiveList(id);
    this.getMyCollect(id);
  },

  //获取活动信息
  async getActiveList(params) {
    const res = await request({ url: "/activites/findAllCampusActivites" });

    //所有活动数据
    let allActives = res.data.data;
    let id = params;
    let singleActiveDetailArr = [];

    let singleActiveDetail = allActives.find((obj) => obj.active_id === id);
    singleActiveDetailArr.push(singleActiveDetail);

    //获取到单独的活动数据

    console.log("单个活动数据", singleActiveDetail);

    this.setData({
      singleActiveDetailArr: singleActiveDetailArr.map((v) => ({
        ...v,
        //开始报名
        registration_start_time_cn: this.fromData(v.registration_start_time),
        //结束报名
        registration_end_time_cn: this.fromData(v.registration_end_time),
        //活动开始
        active_start_time_cn: this.fromData(v.active_start_time),
        //活动结束
        active_end_time_cn: this.fromData(v.active_end_time),
      })),
    });
  },

  //时间戳处理函数，时间戳转换成正常数据
  fromData(a) {
    let time = new Date(a);
    let month = time.getMonth() + 1;
    let date = time.getDate();
    let hours = time.getHours();
    let minutes = time.getMinutes();
    // let second = time.getSeconds() // 获取秒数
    // 小于10前加一个0
    if (month < 10) {
      month = "0" + month;
    }
    if (date < 10) {
      date = "0" + date;
    }
    return time.getFullYear() + "-" + month + "-" + date;
  },

  //获取我的收藏数据
  async getMyCollect(params) {
    const res = await request({ url: "/user/myFavorites?wx_user_id=1" });
    //判断活动id 是否在收藏id里
    let coollectRes = res.data.data.some((v) => v.active_id == params);
    this.setData({
      isCollect: coollectRes,
    });
  },
});
